package cn.gqr.service;

import cn.gqr.dao.*;
import cn.gqr.entity.*;
import cn.gqr.uitl.SessionUitl;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BlogServiceImpl implements BlogService{

    @Autowired
    BolgsDao bd;

    @Autowired
    ContentDao cd;

    @Autowired
    CollectDao cld;

    @Autowired
    UserDao ud;

    @Autowired
    TypeDao td;

    //分页类型查找或者分页模糊查找
    @Override
    public Map findByPage(int start, String title,String typeId) {

        List<Blog> blogs = null;
        long total = 0;

        if( typeId == null ){
            blogs =  bd.findByTitleOrTypePage(start, title,null);
            total = bd.findByTitlePageCount(title,null,Blog.class);
        }

        if( title == null ){
            blogs =  bd.findByTitleOrTypePage(start, null,typeId);
            total = bd.findByTitlePageCount(null,typeId,Blog.class);
        }

        if( title == null && typeId == null ){
            blogs =  bd.findByTitleOrTypePage(start, null,null);
            total = bd.findByTitlePageCount(null,null,Blog.class);
        }

        List<BlogUser> lst = new ArrayList();

        blogs.forEach((blog)->{
            User usr = ud.findById(blog.getUid());
            lst.add(new BlogUser(blog,usr));
        });


        Map map = new HashMap<>();
        map.put("blogs",lst);
        System.out.println("--------" + total);
        map.put("total",total);

        return map;
    }

    //添加博客
    @Override
    public Blog addBlog(BlogContent blog) {

        //添加文章
        String cid = cd.addContent(blog.getContent()).getId();

        //设置博客的文章id和设置发表博客的用户id
        blog.getBlog().setTextId(cid);
        blog.getBlog().setUid(SessionUitl.user().getId());

        //添加博客
        Blog b = bd.addBlog(blog.getBlog());

        //对对应分类进行数量加一操作
        //查询分类
        Type type = td.findById(blog.getBlog().getTypeId());

        //修改数量
        td.upByIdNum(type,true);

        return b;
    }


    //根据博客id查找文章用户类型等信息
    @Override
    public BlogConTypeUser findByIdBlog(String blogId) {

        //获取id对应的博客
        Blog blog = bd.findById(blogId);

        //获取文章内容
        Content content = cd.findByContextId(blog.getTextId());

        //获取这个博客对应的类型
        Type type = td.findById(blog.getTypeId());

        //获取博客对应的用户信息
        User usr = ud.findById(blog.getUid());

        return new BlogConTypeUser(blog, content,type,usr);

    }

    //添加阅读量
    @Override
    public boolean addReadCount(String id) {
        Blog blog = bd.findById(id);
        return bd.addReadCount(blog);
    }


    ////根据登录后的用户id查找所有博客
    @Override
    public Map findByUidPage(int start) {
        Map map = new HashMap();

        List<Blog> blogs = bd.findByUidPage(start);
        long total = bd.findByUidPageCount();

        map.put("blogs",blogs);
        map.put("total",total);

        return map;
    }


    //根据内容id查找文章内容
    @Override
    public Content findByContextId(String id) {
        return cd.findByContextId(id);
    }

    //修改文章内容
    @Override
    public boolean upById(Content content) {
        return cd.upById(content);
    }

    //删除博客
    @Override
    public boolean del(Blog blog) {

        //先删除博客
        boolean b = bd.delById(blog.getId());

        //删除文章
        cd.delById(blog.getTextId());

        //类型数量减一
        Type type = td.findById(blog.getTypeId());
        td.upByIdNum(type,false);

        return b;
    }

    //获取登录用户的全部收藏
    @Override
    public Map findByPageCollect(int start) {

        List<Collect> cldAll = cld.findByPage(start);

        List<Blog> lst = new ArrayList<>();

        cldAll.forEach((obj)->{
            Blog blog = bd.findById(obj.getBid());
            lst.add(blog);
        });

        Map map = new HashMap();
        map.put("blogs",lst);
        map.put("total",cld.collectCount());

        return map;
    }

    //取消收藏
    @Override
    public boolean cancelCollect(String bid) {
        //查博客
        Blog blog = bd.findById(bid);
        //减数量
        cld.CollectNum(blog,false);
        //删收藏表
        return cld.cancelCollect(bid);
    }


    //查看该文章是否收藏
    @Override
    public boolean checkCollect(Collect collect) {
        return cld.checkCollect(collect);
    }

    //添加收藏
    @Override
    public Collect addCollect(String bid) {
        //查找博客
        Blog blog = bd.findById(bid);
        //博客数量加一
        cld.CollectNum(blog,true);
        //添加收藏表
        Collect c = cld.addCollect(bid);
        return c;
    }

}
